using micromsg;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Protocol;
using MicroMsg.Storage;
using System;
using System.Windows.Media.Imaging;
namespace MicroMsg.Scene
{
	public class NetSceneGetQRCode : NetSceneBaseEx<GetQRCodeRequest, GetQRCodeResponse, GetQRCodeRequest.Builder>
	{
		private const string TAG = "NetSceneGetQRCode";
		public const string QRStyleKey = "QRStyle";
		public bool doScene()
		{
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(0);
			this.mBuilder.UserName = Util.toSKString(AccountMgr.getCurAccount().strUsrName);
			this.mBuilder.Style = (uint)ConfigMgr.getInt("QRStyle", 0);
			this.mSessionPack.mCmdID = 67;
			base.endBuilder();
			return true;
		}
		protected override void onFailed(GetQRCodeRequest request, GetQRCodeResponse response)
		{
			Log.smethod_1("NetSceneGetQRCode", "send GetQRCodeRequest failed");
			EventCenter.postEvent(EventConst.ON_NETSCENE_GET_QRCODE_ERR, RetConst.MM_ERR_CLIENT, null);
		}
		protected override void onSuccess(GetQRCodeRequest request, GetQRCodeResponse response)
		{
			RetConst ret = (RetConst)response.BaseResponse.Ret;
			if (ret != RetConst.MM_OK)
			{
				Log.smethod_1("NetSceneGetQRCode", "send GetQRCodeRequest failed ret =" + ret);
				EventCenter.postEvent(EventConst.ON_NETSCENE_GET_QRCODE_ERR, ret, null);
				return;
			}
			ConfigMgr.setInt("QRStyle", (int)response.Style);
			StorageIO.writeToFile(this.getQRFileName(), response.QRCode.Buffer.ToByteArray(), false);
			EventCenter.postEvent(EventConst.ON_NETSCENE_GET_QRCODE_SUCCESS, ret, null);
		}
		public string getQRFileName()
		{
			return StorageIO.getAccountPath(AccountMgr.getCurAccount().strUsrName) + "/QRCode.jpg";
		}
		public BitmapImage getQRImg()
		{
			byte[] array = StorageIO.readFromFile(this.getQRFileName());
			if (array == null)
			{
				return null;
			}
			return ImageCacheMgr.loadFromBuf(array);
		}
	}
}
